using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections;
using Widgetsphere.Core.Exceptions;
using Widgetsphere.Core.DataAccess;
using Acme.Sales.Business;
using Acme.Sales.Business.Objects;
using Acme.Sales.Business.Rules;
using Acme.Sales.Business.SelectCommands;
using System.Collections.Generic;

namespace Acme.Sales.Domain.Objects
{
	/// <summary>
	/// This is an customizable extender for the domain class associated with the 'Customer' object
	/// </summary>
	[Serializable()]
	partial class DomainCustomer : PersistableDomainObjectBase
	{
		#region Member Variables

		private const string TABLE_NAME = "CustomerCollection";
		internal DomainCustomerCollection ParentCol;

		#endregion

		#region Constructor

		internal DomainCustomer(DataRowBuilder rb) : base(rb) 
		{
			this.ParentCol = ((DomainCustomerCollection)(base.Table));
		}

		#endregion

		#region Properties

		internal CustomerPrimaryKey PrimaryKey
		{
			get { return new CustomerPrimaryKey(this.UserId); }
		}

		internal string Address
		{
			get
			{
				try
				{
					if (base[this.ParentCol.AddressColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.AddressColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.AddressColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.AddressColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.Address = value;
				}
			}
		}

		internal string City
		{
			get
			{
				try
				{
					if (base[this.ParentCol.CityColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.CityColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.CityColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.CityColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.City = value;
				}
			}
		}

		internal string Country
		{
			get
			{
				try
				{
					if (base[this.ParentCol.CountryColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.CountryColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.CountryColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.CountryColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.Country = value;
				}
			}
		}

		internal string Fax
		{
			get
			{
				try
				{
					if (base[this.ParentCol.FaxColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.FaxColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.FaxColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.FaxColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.Fax = value;
				}
			}
		}

		internal string FirstName
		{
			get
			{
				try
				{
					if (base[this.ParentCol.FirstNameColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.FirstNameColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (value == null) throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_SETNULL);

			  if (!base[this.ParentCol.FirstNameColumn].Equals(value))
				{
					base[this.ParentCol.FirstNameColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.FirstName = value;
				}
			}
		}

		internal string LastName
		{
			get
			{
				try
				{
					if (base[this.ParentCol.LastNameColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.LastNameColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (value == null) throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_SETNULL);

			  if (!base[this.ParentCol.LastNameColumn].Equals(value))
				{
					base[this.ParentCol.LastNameColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.LastName = value;
				}
			}
		}

		internal string MiddleName
		{
			get
			{
				try
				{
					if (base[this.ParentCol.MiddleNameColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.MiddleNameColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.MiddleNameColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.MiddleNameColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.MiddleName = value;
				}
			}
		}

		internal string Phone
		{
			get
			{
				try
				{
					if (base[this.ParentCol.PhoneColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.PhoneColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.PhoneColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.PhoneColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.Phone = value;
				}
			}
		}

		internal string PostalCode
		{
			get
			{
				try
				{
					if (base[this.ParentCol.PostalCodeColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.PostalCodeColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.PostalCodeColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.PostalCodeColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.PostalCode = value;
				}
			}
		}

		internal string Region
		{
			get
			{
				try
				{
					if (base[this.ParentCol.RegionColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.RegionColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.RegionColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.RegionColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.UserId);
					if (item != null) item.Region = value;
				}
			}
		}

		internal int UserId
		{
			get
			{
				try
				{
					return ((int)(base[this.ParentCol.UserIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		#endregion

		#region Collection Operation Methods

		/// <summary>
		/// Serves as a hash function for this particular type.
		/// </summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (obj.GetType() != this.GetType()) return false;

			if ((this.RowState == DataRowState.Deleted) ||
				(((DataRow)obj).RowState == DataRowState.Deleted) ||
				(this.RowState == DataRowState.Detached) ||
				(((DataRow)obj).RowState == DataRowState.Detached))
				return false;

			return ((DomainCustomer)obj).PrimaryKey.Equals(this.PrimaryKey);
		}

		public override void Persist()
		{
			DomainHelper.PersistDomainObject(this, this.ParentCol.SubDomain, Collections.CustomerCollection, this.ParentCol.Modifier);
		}

		public override void Remove()
		{
			this.ParentCol.RemoveCustomer(this);
		}

		#endregion

		#region Relationship Methods
		internal virtual void ReleaseNonIdentifyingRelationships()
		{
		}

		#endregion
	}
}
